package com.employee.controller;

import com.employee.pojo.dto.UserDTO;
import com.employee.pojo.result.Result;
import com.employee.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.security.SecureRandom;

@RestController
public class EmailController {

    @Autowired
    private EmailService emailService;

    // 生成强密码
    public static String generateStrongPassword() {
        // 定义强密码字符集
        String upperCaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String lowerCaseLetters = "abcdefghijklmnopqrstuvwxyz";
        String digits = "0123456789";
        String specialCharacters = "!@#$%^&*()-_+=<>?";

        // 组合所有字符集
        String allCharacters = upperCaseLetters + lowerCaseLetters + digits + specialCharacters;

        // 使用 SecureRandom 生成更安全的随机数
        SecureRandom random = new SecureRandom();

        // 定义密码长度
        int passwordLength = 12;

        // 构建密码
        StringBuilder password = new StringBuilder(passwordLength);
        for (int i = 0; i < passwordLength; i++) {
            int index = random.nextInt(allCharacters.length());
            password.append(allCharacters.charAt(index));
        }

        return password.toString();
    }



    @PostMapping("/sendEmail")
        public Result<String> sendEmail(@RequestBody UserDTO email) {
        System.out.println(email);
        String captcha = generateStrongPassword();
        System.out.println(captcha);
        return Result.success(emailService.sendVerificationEmail(email.getEmail(), captcha));
    }

}
